let dataMgr = require("./DataMgr");
cc.Class({
    extends: cc.Component,

    properties: {
        audioClips: [cc.AudioClip]
    },


    onLoad() {
        this.isRun = true;
        this.iskeyDown = false;
        //注册键盘事件
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        //获取动画组件
        this.animationComp = this.node.getComponent(cc.Animation);
        this.rigidbodyComp = this.node.getComponent(cc.RigidBody);
        //给玩家向下的力，使玩家具有向下的加速度
        this.force = -6000;
        //给玩家设置速度，使得玩家
        this.addSpeed = 148;
        this.initSpeed = 0;
        //获取碰撞组件
        let phyPolygonColComp = this.node.getComponent(cc.PhysicsPolygonCollider);
        this.density = phyPolygonColComp.density;
        let m = this.density * this.node.width * this.node.height;
        this.aSpeed = this.force / m;
        this.isheight = false;
        this.isheightSpeed = false;
    },

    start() {

    },

    update(dt) {
        if (!this.isRun) {
            this.initSpeed += this.aSpeed * dt;
            this.node.y += this.initSpeed * dt;
            if (this.node.y >= cc.winSize.height / 4) {
                this.isheight = true;
            } else {
                this.isheight = false;
            }
        }

        if (this.node.x <= -cc.winSize.width / 2) {
            cc.audioEngine.stopMusic();
            cc.director.loadScene("Over");
        }
    },
    onKeyDown: function (event) {
        switch (event.keyCode) {
            case cc.macro.KEY.w:
                if (this.iskeyDown) {
                    return;
                }
                this.iskeyDown = true;
                this.animationComp.pause();
                //避免玩家的速度太高
                if (this.initSpeed >= this.addSpeed) {
                    return;
                }
                this.initSpeed += this.addSpeed;
                if (this.isRun === true) {
                    this.isRun = false;
                }
                cc.audioEngine.playEffect(this.audioClips[0], false);
                break;

        }
    },

    onKeyUp: function (event) {
        switch (event.keyCode) {
            case cc.macro.KEY.w:
                this.iskeyDown = false;
                break;
        }
    },
    onBeginContact: function (contact, selfCollider, otherCollider) {
        if ((selfCollider.tag === 0 && otherCollider.node.group === "ground" && this.isRun === false)
            || (selfCollider.tag === 0 && otherCollider.node.group === "platform" && this.isRun === false)) {
            this.isRun = true;
            this.initSpeed = 0;
            this.animationComp.resume();
        }
        else if (selfCollider.tag === 1 && otherCollider.node.group === "platform") {
            dataMgr.getInstance().saveData("isOver", true);
            this.node.removeFromParent();
            cc.audioEngine.stopMusic();
            cc.director.loadScene("Over");
        }
    },




    onDestroy() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_Up, this.onKeyUp, this);
    },



});
